package com.dc.di.ftpserver.command;

import java.io.IOException;

import org.apache.ftpserver.command.AbstractCommand;
import org.apache.ftpserver.ftplet.DefaultFtpReply;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpReply;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.impl.FtpIoSession;
import org.apache.ftpserver.impl.FtpServerContext;

import com.dc.di.ftpserver.util.Const;
import com.dc.di.ftpserver.util.CustomCommandHelper;

public class PARAM extends AbstractCommand {

	@Override
	public void execute(FtpIoSession session, FtpServerContext context,
			FtpRequest request) throws IOException, FtpException {
		if(request.getArgument() != null) {
			 String paramName=CustomCommandHelper.getParameterName(request.getArgument());
			 String paramValue=CustomCommandHelper.getParameterValue(request.getArgument());
			 if(paramName!=null && paramValue!=null){
				 session.setAttribute(Const.PARAM_PREFIX.concat(paramName), paramValue);
				 session
	           .write(new DefaultFtpReply(FtpReply.REPLY_200_COMMAND_OKAY, "set "+paramName+" is ok"));
			 }else{
				 session
		           .write(new DefaultFtpReply(FtpReply.REPLY_504_COMMAND_NOT_IMPLEMENTED_FOR_THAT_PARAMETER, "Argument is null"));
			 }			
		}else{
			session
	           .write(new DefaultFtpReply(FtpReply.REPLY_504_COMMAND_NOT_IMPLEMENTED_FOR_THAT_PARAMETER, "Argument is null"));
		}

	}
	public static String getCommmand(){
		return "PARAM";
	}
}
